package cn.classTest.mall.controller;

import cn.classTest.mall.domain.Employee;
import cn.classTest.mall.domain.Mall;
import cn.classTest.mall.domain.Shop;
import cn.classTest.mall.query.QueryObject;
import cn.classTest.mall.service.EmployeeService;
import cn.classTest.mall.service.MallService;
import cn.classTest.mall.service.ShopService;
import com.github.pagehelper.PageInfo;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MallService mallService;

    /*
     * DepartmentController:list
     * */
    @RequiresPermissions(value = {"shop:list", "店铺列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Shop> query = shopService.query(qo);
        model.addAttribute("pageInfo", query);
        return "shop/list";
    }

    /*
     * DepartmentController:input
     * */

    @RequiresPermissions(value = {"shop:input", "店铺编辑"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        List<Mall> malls = mallService.selectAll();
        model.addAttribute("malls", malls);
        List<Employee> employees = employeeService.selectAll();
        model.addAttribute("employees", employees);

        if (id != null) {
            Shop shop = shopService.selectOne(id);
            model.addAttribute("oldShop", shop);
        }
        return "/shop/input";
    }

    /*
     * DepartmentController:saveOrUpdate
     * */
    @RequiresPermissions(value = {"shop:saveOrUpdate", "店铺新增或编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Shop shop) {
        shopService.saveOrUpdate(shop);
        return "redirect:/shop/list";
    }

    @RequiresPermissions(value = {"shop:delete", "店铺删除"}, logical = Logical.OR)
    @RequestMapping("/delete")
    public String delete(Long id) {
        shopService.delete(id);
        return "redirect:/shop/list";
    }

}
